package com.dn.luckbi.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    // redis域名
    private String host;

    // redis端口
    private String port;

    // redis使用库
    private int database;

    // redis使用库
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        // 1. 创建配置
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s",host,port);
        config.useSingleServer()
                .setAddress(redisAddress)
                .setPassword(password)
                .setDatabase(database);
        // 2. 创建Redisson实例
        // Sync and Async API
        RedissonClient redisson = Redisson.create(config);
        // 3. 返回
        return redisson;
    }

}